package com.wanxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.LawyerModel;
import com.wanxi.model.UserModel;
import com.wanxi.service.LawyerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Li
 * @Date 2023/7/7 19:58
 */
@RestController
@Api(tags = "律师管理",value = "用户Open api 入口")
@RequestMapping("/back/lawyer")
public class LawyerController {
    @Autowired
    private LawyerService lawyerService;

    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询所有律师信息",response = LawyerModel.class,httpMethod = "GET")
    public ResultDto findAll(LawyerModel lawyerModel) {
        IPage<LawyerModel> page = new Page<>(lawyerModel.getPage(), lawyerModel.getLimit());
        QueryWrapper<LawyerModel> queryWrappers = query(lawyerModel);
        IPage<LawyerModel> iPage = lawyerService.page(page, queryWrappers);
        return ResultDto.builder().code(0).data(iPage.getRecords()).count((int) iPage.getTotal()).build();
    }

    @PostMapping("/addLawyer")
    @PreAuthorize("hasAnyAuthority('add')")//权限
    @ApiOperation(value = "新增律师信息",response = LawyerModel.class,httpMethod = "POST")
    public ResultDto addLawyer(LawyerModel lawyerModel) {
        lawyerService.save(lawyerModel);
        return ResultDto.builder()
                .code(0)
                .msg("新增成功！")
                .build();
    }

    @PostMapping("/updateLawyer")
    @PreAuthorize("hasAnyAuthority('update')")//权限
    @ApiOperation(value = "修改律师信息",response = LawyerModel.class,httpMethod = "POST")
    public ResultDto updateLawyer(LawyerModel lawyerModel) {
        lawyerService.updateById(lawyerModel);
        return ResultDto.builder()
                .code(0)
                .msg("修改成功！")
                .build();
    }

    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询律师信息",response = LawyerModel.class,httpMethod = "GET")
    public ResultDto findById(LawyerModel lawyerModel) {
        LawyerModel lawyers = lawyerService.getById(lawyerModel.getId());
        return ResultDto.builder().data(lawyers).build();
    }

    @PostMapping("/deleteLawyer")
    @PreAuthorize("hasAnyAuthority('delete')")//权限
    @ApiOperation(value = "删除律师信息",response = LawyerModel.class,httpMethod = "POST")
    public ResultDto deleteLawyer(LawyerModel lawyerModel) {
        lawyerService.removeById(lawyerModel.getId());
        return ResultDto.builder()
                .code(0)
                .msg("删除成功")
                .build();
    }

    public QueryWrapper<LawyerModel> query(LawyerModel lawyerModel) {
        QueryWrapper<LawyerModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(lawyerModel.getName() == null), "name", lawyerModel.getName());
        queryWrapper.like(!(lawyerModel.getPhone() == null), "phone", lawyerModel.getPhone());
        queryWrapper.like(!(lawyerModel.getSex()==null),"sex",lawyerModel.getSex());
        queryWrapper.like(!(lawyerModel.getRole()==null),"role",lawyerModel.getRole());
        queryWrapper.between(!(lawyerModel.getStartAge()==null&&lawyerModel.getEndAge()==null),"age",lawyerModel.getStartAge(),lawyerModel.getEndAge());
        queryWrapper.eq(lawyerModel.getEnable()!=null&&lawyerModel.getEnable()!=-1,"enable",lawyerModel.getEnable());
        return queryWrapper;
    }
}
